/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aptech.business;

import com.aptech.DAO.AccountDAO;
import com.aptech.domain.JPA.Account;
import com.aptech.domain.JPA.EventRequest;
import com.aptech.DAO.IAccountDAO;
import com.aptech.domain.model.AccountDO;
import com.aptech.util.Converter;
import com.googlecode.genericdao.search.Search;
import com.googlecode.genericdao.search.SearchResult;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

/**
 *
 * @author Admin
 */
public class AccountManager extends BaseManager implements IAccountManager {
    private static Logger logger = Logger.getLogger(AccountManager.class);
    private IAccountDAO accountDAO;

    public List<AccountDO> getAllAccount() throws Exception {
        List<AccountDO> result = new ArrayList<AccountDO>();        
        List<Account> accountList = accountDAO.findAll();
        if(accountList != null) {
            for (Account account : accountList) {
                result.add(getAccount(account));
            }
        }
        return result;
    }
    public Account saveAccount(Account account) throws Exception {
        final String LOCATION = "saveAccount()";
        try {
            String password = encodeString(account.getPassword());
            System.out.println(password);
            account.setPassword(password);
            accountDAO.save(account);
            return account;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public boolean isUsernameExist(String username) {
        List<Account> accList = accountDAO.findByUsername(username, null);
        return (accList.size() > 0 && accList != null);
    }
    public Set<EventRequest> getEventRequestByUsername(String username) {
        List<Account> accountList = accountDAO.findByUsername(username, null);
        Set<EventRequest> eventList = accountList.get(0).getEventRequests();
        return  eventList;
    }
    /**
     * @param accountDAO the accountDAO to set
     */
    @Override
    public void setAccountDAO(IAccountDAO accountDAO) {
        this.accountDAO = accountDAO;
    }
    public List<Account> getAccount(){
                Search search = new Search();                
                SearchResult<Account> result = accountTestDAO.searchAndCount(search);
                List<Account> accountList = result.getResult();
		return accountList;
    }
    public static void main(String[] args) {
        AccountDAO accountDAO = new AccountDAO();
        List<Account> accountList = accountDAO.getAccount();
        System.out.println(accountList);
        if(accountList != null && accountList.size() > 0) {
            System.out.println(accountDAO.getAccount().get(0).getUsername());
        } else {
            System.out.println("loi");
        }

    }
    public User getPrincipal(HttpSession session) {
    // Do nothing with the session because Spring give us a better way to retrieve the principal
      return (User) ((SecurityContext)
        SecurityContextHolder.getContext()).getAuthentication().getPrincipal();
    }
    public int getAccountId(String username) {
        if(username != null ) {
            Account a = accountDAO.findByUsername(username, null).get(0);
            return a.getAccountId();
        }
        return -1;
    }
    public AccountDO getAccountByUsername(String username) {
        AccountDO accountResult = new AccountDO();
        try {
            Search search = new Search();
            search.addFilterEqual("username", username);
            Account account = (Account)commonDAO.searchUnique(search);
            accountResult = Converter.get(account, AccountDO.class);
        } catch(Exception e)  {
            logger.debug(e);
        }
        return accountResult;
    }
    public AccountDO getCurLoginAcc() {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String userName = user.getUsername();
        return getAccountByUsername(userName);
    }
}
